﻿Public Class Frm1ItemMenuRMP
    Public Sub cargarRegistros(ByVal fila As Integer)
        Try
            Dim accion As New ItemsDAO
            'Llenar tabla
            Dim tabla As New DataTable
            tabla = accion.mostrar
            'Comprobar si hay datos
            If Not tabla Is Nothing Then
                'Cargar datos en el grid
                'DGV1.DataSource = tabla
                FormatosGrid.funciones.formatoGrid_Items(DGV1, tabla)
                'Mostrar datos en los controles
                txtId.Text = DGV1.Rows(fila).Cells(0).Value
                txtNombre.Text = DGV1.Rows(fila).Cells(1).Value
                txtOrigen.Text = DGV1.Rows(fila).Cells(2).Value
                txtPresentacion.Text = DGV1.Rows(fila).Cells(3).Value
                txtPeso.Text = DGV1.Rows(fila).Cells(4).Value
                txtTalla.Text = DGV1.Rows(fila).Cells(5).Value
                txtCosto.Text = DGV1.Rows(fila).Cells(6).Value
                txtTipo.Text = DGV1.Rows(fila).Cells(7).Value
                lblIDtipo.Text = DGV1.Rows(fila).Cells(8).Value
                'Seleccionar toda la fila
                DGV1.Rows(fila).Selected = True
                DGV1.CurrentCell = DGV1.Rows(fila).Cells(1)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                        MessageBoxIcon.Warning)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Frmitemnuevo As New Frm1ItemNuevo
        Frmitemnuevo.Show()
        Me.Close()

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV1.CellContentClick

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Frm1ItemsEditar.Show(Me.txtId.Text, Me.txtNombre.Text, Me.txtOrigen.Text, Me.txtPresentacion.Text, Me.txtPeso.Text, Me.txtTalla.Text, Me.txtCosto.Text, Me)

        Me.Close()


    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim frmitenbuscar As New Frm1ItemBuscar
        frmitenbuscar.Show()

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.Close()

    End Sub

    Private Sub Frm1ItemMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargarRegistros(0)
        txtId.Enabled = False
        txtNombre.Enabled = False
        txtOrigen.Enabled = False
        txtPresentacion.Enabled = False
        txtPeso.Enabled = False
        txtTalla.Enabled = False
        txtCosto.Enabled = False
        txtTipo.Enabled = False

    End Sub
    Private Sub dgv1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGV1.CurrentCellChanged
        Try
            If Not DGV1.CurrentCell Is Nothing Then
                With DGV1
                    txtId.Text = .Rows(.CurrentCell.RowIndex).Cells(0).Value
                    txtNombre.Text = .Rows(.CurrentCell.RowIndex).Cells(1).Value
                    txtOrigen.Text = .Rows(.CurrentCell.RowIndex).Cells(2).Value
                    txtPresentacion.Text = .Rows(.CurrentCell.RowIndex).Cells(3).Value
                    txtPeso.Text = .Rows(.CurrentCell.RowIndex).Cells(4).Value
                    txtTalla.Text = .Rows(.CurrentCell.RowIndex).Cells(5).Value
                    txtCosto.Text = .Rows(.CurrentCell.RowIndex).Cells(6).Value
                    txtTipo.Text = .Rows(.CurrentCell.RowIndex).Cells(7).Value
                    lblIDtipo.Text = .Rows(.CurrentCell.RowIndex).Cells(8).Value
                End With
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
              MessageBoxIcon.Warning)
        End Try
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim dato As New Items
        Dim accion As New ItemsDAO
        Dim resp As DialogResult
        dato.pIdProd = txtId.Text
        dato.pNombre = txtNombre.Text
        dato.pOrigen = txtOrigen.Text
        dato.pPresentacion = txtPresentacion.Text
        dato.pPeso = txtPeso.Text
        dato.pTalla = txtTalla.Text
        dato.pCosto = txtCosto.Text
        dato.pTipo = lblIDtipo.Text


        resp = MessageBox.Show("Desea eliminar el registro " & dato.pNombre, "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        If resp = Windows.Forms.DialogResult.Yes Then
            If accion.eliminar(dato) Then
                MessageBox.Show("Registro eliminado", "Borrar", MessageBoxButtons.OK, MessageBoxIcon.Information)
                cargarRegistros(0)
            Else
                MessageBox.Show("No se puede eliminar el registro, contacte al administrador", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If
        End If
    End Sub

End Class